package com.example.whac_a_mole;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataBase {
    private Context context;
    private MySQLiteOpenHelper helper;
    //创建表
    public DataBase(Context context) {
        this.context = context;
        helper=new MySQLiteOpenHelper(context,"User.db",null,2);
    }

    private class MySQLiteOpenHelper extends SQLiteOpenHelper {
        private String CREAT_TABLE="create table User(\n" +
                "id integer primary key autoincrement,\n" +
                "user text,\n" +
                "passwd text\n" +
                ");";
        private String CREATE_TABLE_TOP="create table Top(\n" +
                "id integer primary key autoincrement,\n" +
                "mode text,\n" +
                "user text,\n" +
                "time1 text,\n" +
                "mark integer,\n" +
                "time2 integer\n" +
                ")";
        private String INIT="insert into User(user,passwd) values(\"admin\",\"admin\"),\n" +
                "(\"lsk\",\"lsk1998\"),\n" +
                "(\"19829088780\",\"lsk1998\");";

        public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }


        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL(CREAT_TABLE);
            sqLiteDatabase.execSQL(INIT);
            sqLiteDatabase.execSQL(CREATE_TABLE_TOP);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        }
    }
    //注册新用户
   public  void submit(String user,String password)
    {
        SQLiteDatabase db=helper.getWritableDatabase();
        db.execSQL("insert into User(user,passwd) values(?,?)",new String[]{user,password});
        /*
        ContentValues values=new ContentValues();
        values.put("user",user);
        values.put("passwd",password);
        long num = db.insert("User", null, values);
        */
        db.close();
    }
    //登录
    public int login(String user,String passwd)
    {
        SQLiteDatabase db=helper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from User where user =? and passwd=?",new String[]{user,passwd});
        int count=cursor.getCount();
        cursor.close();
        db.close();
        return count;
    }
    //修改密码
    public void setpasswd(String user,String oldpd,String newpd)
    {
       SQLiteDatabase db = helper.getWritableDatabase();
       db.execSQL("update User set passwd=? where user=?",new String[]{newpd,user});
       db.close();
    }
    /*
    下面的方法是关于高分榜的
     */
    //新增
    public void insert(String mode,String user,String time1,int mark,int time2)
    {
        SQLiteDatabase db=helper.getWritableDatabase();
        db.execSQL("insert into Top(mode,user,time1,mark,time2) values(?,?,?,?,?);",new String[]{mode,user,time1,String.valueOf(mark),String.valueOf(time2)});
        db.close();
    }
    //排序查找
    public List<Map<String,String>> selectall()
    {
        SQLiteDatabase db=helper.getWritableDatabase();
        //Cursor cursor=db.rawQuery("select mode,user,time1,mark,time2 from Top order by mark/time2;",null);
        Cursor cursor=db.rawQuery("select mode,user,time1,mark,time2 from Top where mark!=0 order by mark desc",null);
        //将返回值存储到List<Map<String,Object>>集合
        List<Map<String,String>> list=new ArrayList<>();

        /*
        表头
        */
        HashMap<String,String> map1=new HashMap<>();
        map1.put("num","排名");
        map1.put("mode","模式");
        map1.put("user","用户");
        map1.put("time1","创建时间");
        map1.put("mark","得分");
        map1.put("time2","耗时");
        list.add(map1);

        int num=1;
        if(cursor.moveToFirst())
        {
            do
                {
                    HashMap<String,String> map=new HashMap<>();
                    map.put("num",String.valueOf(num++));
                    map.put("mode",cursor.getString(cursor.getColumnIndex("mode")));
                    map.put("user",cursor.getString(cursor.getColumnIndex("user")));
                    map.put("time1",cursor.getString(cursor.getColumnIndex("time1")));
                    map.put("mark",String.valueOf(cursor.getInt(cursor.getColumnIndex("mark"))));
                    map.put("time2",String.valueOf(cursor.getInt(cursor.getColumnIndex("time2"))));
                    list.add(map);
                }while(cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return list;
    }








}
